//#version 420

uniform sampler2DRect uWave;
uniform sampler2DRect uPrevWave;
uniform sampler2DRect uWaveStepConst;

in vec2 vTexCoords;

out vec4 fValue;

void main()
{
    // Move the sampling location in case we're on boundary (to preserve Neumann boundary conditions)
    ivec2 offset=ivec2(vTexCoords);

/*
    ivec2 txSize = textureSize(uWave).xy;
    if(vTexCoords.x == 0)
        offset.x+=2;
    else if(vTexCoords.x == txSize.x-1)
        offset.x-=2;
    else if(vTexCoords.y == 0)
        offset.y+=2;
    else if(vTexCoords.y == txSize.y-1)
        offset.y-=2;
*/

    float waveStepConst=texelFetch(uWaveStepConst, offset).r;
    float tmp=texelFetchOffset(uWave, offset, ivec2(0,1)).r;
    tmp+=texelFetchOffset(uWave, offset, ivec2(0,-1)).r;
    tmp+=texelFetchOffset(uWave, offset, ivec2(1,0)).r;
    tmp+=texelFetchOffset(uWave, offset, ivec2(-1,0)).r;
    tmp*=5;
    tmp-=texelFetchOffset(uPrevWave, offset, ivec2(0,1)).r;
    tmp-=texelFetchOffset(uPrevWave, offset, ivec2(0,-1)).r;
    tmp-=texelFetchOffset(uPrevWave, offset, ivec2(1,0)).r;
    tmp-=texelFetchOffset(uPrevWave, offset, ivec2(-1,0)).r;
    float prevTex=texelFetch(uPrevWave, offset).r;
    tmp+=4*prevTex;
    tmp*=waveStepConst*0.25;

    float tmp1=texelFetch(uWave, offset).r;
    tmp1*=2-5*waveStepConst;

    tmp+=tmp1-prevTex;
    fValue.r = tmp;
}